package com.dingpeikeji.quickdeer.modules.rcpt.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.DateUtils;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.modules.rcpt.entity.TRcpt;
import com.dingpeikeji.quickdeer.modules.rcpt.service.TRcptService;

/**
 * 发票信息Controller
 * @author dingpei
 * @version 2017-03-03
 */
@Controller
@RequestMapping(value = "${adminPath}/rcpt/tRcpt")
public class TRcptController extends BaseController {

	@Autowired
	private TRcptService tRcptService;
	
	@ModelAttribute
	public TRcpt get(@RequestParam(required=false) String id) {
		TRcpt entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tRcptService.get(id);
		}
		if (entity == null){
			entity = new TRcpt();
		}
		return entity;
	}
	
	@RequiresPermissions("rcpt:tRcpt:view")
	@RequestMapping(value = {"list", ""})
	public String list(TRcpt tRcpt, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TRcpt> page = tRcptService.findPage(new Page<TRcpt>(request, response), tRcpt); 
		model.addAttribute("page", page);
		return "modules/rcpt/tRcptList";
	}

	@RequiresPermissions("rcpt:tRcpt:view")
	@RequestMapping(value = "form")
	public String form(TRcpt tRcpt, Model model) {
		model.addAttribute("tRcpt", tRcpt);
		return "modules/rcpt/tRcptForm";
	}

	@RequiresPermissions("rcpt:tRcpt:edit")
	@RequestMapping(value = "save")
	public String save(TRcpt tRcpt, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tRcpt)){
			return form(tRcpt, model);
		}
		tRcptService.save(tRcpt);
		addMessage(redirectAttributes, "保存发票信息成功");
		return "redirect:"+Global.getAdminPath()+"/rcpt/tRcpt/?repage";
	}
	
	@RequiresPermissions("rcpt:tRcpt:edit")
	@RequestMapping(value = "confirm")
	public String confirm(TRcpt tRcpt, Model model, RedirectAttributes redirectAttributes) {
		tRcpt.setRcptStatus(TRcpt.RCPT_ST_CHECKED);
		tRcptService.confirm(tRcpt);
		addMessage(redirectAttributes, "保存发票信息成功");
		return "redirect:"+Global.getAdminPath()+"/rcpt/tRcpt/?repage";
	}
	
	@RequiresPermissions("rcpt:tRcpt:edit")
	@RequestMapping(value = "delete")
	public String delete(TRcpt tRcpt, RedirectAttributes redirectAttributes) {
		tRcptService.delete(tRcpt);
		addMessage(redirectAttributes, "删除发票信息成功");
		return "redirect:"+Global.getAdminPath()+"/rcpt/tRcpt/?repage";
	}

}